/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2022-12-31
 * Time: 2:14
 */
public class Solution4 {
    // https://leetcode.cn/problems/guess-number-higher-or-lower/
    public static int guess(int num) {
        if (num == pick) {
            return 0;
        }
        return num > pick ? -1 : 1;
    }
    public static int guessNumber(int n) {
        if (0 == guess(n)) {
            return n;
        }
        int l = 0;
        int r = n;
        while (l <= r) {
            int mid = l+(r-l)/2;
            if (guess(mid) > 0) {
                l = mid;
            } else if (guess(mid) < 0) {
                r = mid;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static final int pick = 2;
    public static void main(String[] args) {
        System.out.println(guessNumber(2));
    }
}
